function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('-') + '-' + [hour, minute, second].map(formatNumber).join('-')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function guid() {
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  }
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
    s4() + '-' + s4() + s4() + s4();
}

function validateEmail(email) {
  return /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(email)
}

function validatePassword(password) {
  return password && password.length >= 8;
}

function validatePhone(phone) {
  return phone && /^1\d{10}$/.test(phone)
}

function validateCode(code) {
  return code && 4 == code.length
}

function j(title,msg) {
  wx.showModal({
    title: title,
    content: msg,
    success: function (res) {
      if (res.confirm) {
        console.log('用户点击确定')
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
}

function i(title, msg) {
  wx.showModal({
    title: title,
    content: msg,
    success: function (res) {
      if (res.confirm) {
        wx.redirectTo({
          url: '../../Visitor/visitors/visitors'
        })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
}

function k(title, msg) {
  wx.showModal({
    title: title,
    content: msg,
    success: function (res) {
      if (res.confirm) {
        wx.redirectTo({
          url: '../Visitor/visitors/visitors'
        })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
}

function p(title, msg) {
  wx.showModal({
    title: title,
    content: msg,
    success: function (res) {
      if (res.confirm) {
        wx.redirectTo({
          url: '../../../pages/Visitor/visitorsindex/visitorsindex'
        })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
}

function a(title, msg) {
  wx.showModal({
    title: title,
    content: msg,
    success: function (res) {
      if (res.confirm) {
        wx.redirectTo({
          url: '../../../pages/Visitor/showuser/showuser' 
        })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
}

function ajax(method, url, data,callback){
 
  wx.request({
    url: this.getBasePath()+url,
    method: method,
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    data: data,
    success: function (res) {
      let data=res.data;
      
  
      if (!data.retCode ){
        wx.showToast({
          title: '网络异常',
          icon: 'fail',
          duration: 2000
        })
        return
      }
      if (data.retCode == "500" ){
        wx.showToast({
          title: data.retMsg,
          icon: 'fail',
          duration: 2000
        })
        return 
      }
      if (data.retCode == "200") {
        
        callback.call(this, data.retDetail);
      }
      

    }, 
    fail(){
    
        wx.showToast({
          title: '网络连接失败',
          icon: 'fail',
          duration: 1000
        })
      
    }
  })
}

function getBasePath(){
  return "http://localhost:8501/api";
}

module.exports = {
  formatTime: formatTime,
  guid: guid,
  validateEmail: validateEmail,
  validatePassword: validatePassword,
  validatePhone: validatePhone,
  validateCode: validateCode,
  msg: j,
  index:i,
  pass:p,
  add:a,
  indexk: k,
  ajax:ajax,
  getBasePath: getBasePath
}